package org.redrider.shop.domain

import grails.converters.*

class PartCategoryController {

    def index = { redirect(action:list,params:params) }

    // the delete, save and update actions only accept POST requests
    static allowedMethods = [delete:'POST', save:'POST', update:'POST']

    def list = {
        params.max = Math.min( params.max ? params.max.toInteger() : 10,  100)
        def pcList = PartCategory.list( params )
        withFormat{
            html [ partCategoryInstanceList: pcList, partCategoryInstanceTotal: PartCategory.count() ]
            json {render pcList as JSON}
        }
    }

    def show = {
        def partCategoryInstance = PartCategory.get( params.id )
        withFormat{
            html{
                if(!partCategoryInstance) {
                    flash.message = "PartCategory not found with id ${params.id}"
                    redirect(action:list)
                }
                else { return [ partCategoryInstance : partCategoryInstance ] }
            }
            json {
                if(!partCategoryInstance) {
                    render [error:"PartCategory not found with id ${params.id}"] as JSON
                }
                else { render partCategoryInstance as JSON }
            }
        }
    }

    def delete = {
        def partCategoryInstance = PartCategory.get( params.id )
        if(partCategoryInstance) {
            try {
                partCategoryInstance.delete(flush:true)
                flash.message = "PartCategory ${params.id} deleted"
                redirect(action:list)
            }
            catch(org.springframework.dao.DataIntegrityViolationException e) {
                flash.message = "PartCategory ${params.id} could not be deleted"
                redirect(action:show,id:params.id)
            }
        }
        else {
            flash.message = "PartCategory not found with id ${params.id}"
            redirect(action:list)
        }
    }

    def edit = {
        def partCategoryInstance = PartCategory.get( params.id )

        if(!partCategoryInstance) {
            flash.message = "PartCategory not found with id ${params.id}"
            redirect(action:list)
        }
        else {
            return [ partCategoryInstance : partCategoryInstance ]
        }
    }

    def update = {
        def partCategoryInstance = PartCategory.get( params.id )
        if(partCategoryInstance) {
            if(params.version) {
                def version = params.version.toLong()
                if(partCategoryInstance.version > version) {

                    partCategoryInstance.errors.rejectValue("version", "partCategory.optimistic.locking.failure", "Another user has updated this PartCategory while you were editing.")
                    render(view:'edit',model:[partCategoryInstance:partCategoryInstance])
                    return
                }
            }
            partCategoryInstance.properties = params
            if(!partCategoryInstance.hasErrors() && partCategoryInstance.save()) {
                flash.message = "PartCategory ${params.id} updated"
                redirect(action:show,id:partCategoryInstance.id)
            }
            else {
                render(view:'edit',model:[partCategoryInstance:partCategoryInstance])
            }
        }
        else {
            flash.message = "PartCategory not found with id ${params.id}"
            redirect(action:list)
        }
    }

    def create = {
        def partCategoryInstance = new PartCategory()
        partCategoryInstance.properties = params
        return ['partCategoryInstance':partCategoryInstance]
    }

    def save = {
        def partCategoryInstance = new PartCategory(params)
        if(!partCategoryInstance.hasErrors() && partCategoryInstance.save()) {
            flash.message = "PartCategory ${partCategoryInstance.id} created"
            redirect(action:show,id:partCategoryInstance.id)
        }
        else {
            render(view:'create',model:[partCategoryInstance:partCategoryInstance])
        }
    }
}
